﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace Matrix.Common.Utility
{
    /// <summary>
    /// 坐标转换
    /// </summary>
  public  class CoordinateTransform
    {
        /// <summary>
        /// wg84转百度
        /// </summary>
        /// <param name="longitude">gps经度</param>
        /// <param name="latitude">gps纬度</param>
        /// <returns>百度经纬度</returns>
        public static double[] WG84_BD(double longitude, double latitude)
        {
            //百度坐标转换API from=0是WG84,from=2是谷歌
            string path = "http://api.map.baidu.com/ag/coord/convert?from=0&to=4&x=" + longitude + "&y=" + latitude + "";
            //WebClient请求
            WebClient client = new WebClient();
            Stream str = client.OpenRead(path);
            StreamReader sr = new StreamReader(str);
            string strResult = sr.ReadToEnd();
            str.Close();
            string lon = CutWord(strResult, "\"x\":\"", "\",");
            string lat = CutWord(strResult, "\"y\":\"", "\"}");

            //进行Base64解码
            byte[] xBuffer = Convert.FromBase64String(lon);
            string strX = Encoding.UTF8.GetString(xBuffer, 0, xBuffer.Length);

            byte[] yBuffer = Convert.FromBase64String(lat);
            string strY = Encoding.UTF8.GetString(yBuffer, 0, yBuffer.Length);
            return new double[] { double.Parse(strX), double.Parse(strY) };
        }



        /// <summary>
        /// 百度转WG84
        /// </summary>
        /// <param name="longitude">百度经度</param>
        /// <param name="latitude">百度纬度</param>
        /// <returns>gps经纬度</returns>
        public static double[] BD_WG84(double longitude, double latitude)
        {
            //百度坐标转换API from=0是WG84,from=2是谷歌
            string path = "http://api.map.baidu.com/ag/coord/convert?from=0&to=4&x=" + longitude + "&y=" + latitude + "";
            //WebClient请求
            WebClient client = new WebClient();
            Stream str = client.OpenRead(path);
            StreamReader sr = new StreamReader(str);
            string strResult = sr.ReadToEnd();
            str.Close();
            string lon = CutWord(strResult, "\"x\":\"", "\",");
            string lat = CutWord(strResult, "\"y\":\"", "\"}");

            //进行Base64解码
            byte[] xBuffer = Convert.FromBase64String(lon);
            string strX = Encoding.UTF8.GetString(xBuffer, 0, xBuffer.Length);

            byte[] yBuffer = Convert.FromBase64String(lat);
            string strY = Encoding.UTF8.GetString(yBuffer, 0, yBuffer.Length);
            decimal x = 2 * decimal.Parse(longitude.ToString()) - decimal.Parse(strX);
            decimal y = 2 * decimal.Parse(latitude.ToString()) - decimal.Parse(strY);
            return new double[] { double.Parse(x.ToString()), double.Parse(y.ToString()) };
        }

        /// <summary>
        /// 文本切割 
        /// </summary>
        /// <param name="source">源文本</param>
        /// <param name="keyWord1">关键词1</param>
        /// <param name="keyWord2">关键词2</param>
        /// <returns>所需文本</returns>
        public static string CutWord(string source, string keyWord1, string keyWord2)
        {
            try
            {
                string returnValue;
                int k1 = source.IndexOf(keyWord1);
                int l1 = keyWord1.Length;
                source = source.Substring(k1 + l1);
                int k2 = source.IndexOf(keyWord2);
                returnValue = source.Substring(0, k2);
                return returnValue;
            }
            catch
            {
                return "";
            }
        }
    }
}
